﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ShareMarking.Entity;
using ShareMarking.Service;

namespace ShareMarking.ShowqrCode
{
    /// <summary>
    /// api 的摘要说明
    /// </summary>
    public class api : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            var method = ComLib.QueryStringHelper.GetStringByQueryString("method");
            switch (method)
            {
                case "login":
                    login(context);
                    break;
                case "getnc":
                    getnc(context);
                    break;
            }
        }

        private void getnc(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string nccode = ComLib.QueryStringHelper.GetFormString("nccode");
            nc_ncuser nc = nc_ncuserService.Instance.Get_SingleEntity_byWhere(nc_ncuser._.nc_code == nccode);
            if (nc != null)
            {
                qr_QrCodeRelate relate = qr_QrCodeRelateService.Instance.Get_SingleEntity_byWhere(
                    qr_QrCodeRelate._.rel_Code == nccode && qr_QrCodeRelate._.IsDelete == false);
                if (relate != null)
                {
                    qr_QrCode qr = qr_QrCodeService.Instance.Get_SingleEntity_byWhere(qr_QrCode._.scene_id == relate.qr_Scene_ID);
                    if (qr != null)
                    {
                        context.Response.Write("{\"status\": 0, \"name\": \"" + nc.name 
                            + "\",\"code_url\": \"" + qr.qrcode_url
                            + "\",\"nclcode\": \"" + nc.ncl_code
                            + "\",\"nclname\": \"" + nc.ncl_name
                            + "\",\"nclmobile\": \"" + nc.ncl_tel
                            + "\",\"mobile\":\""+nc.tel+"\"}");
                        //context.Response.Redirect("show.aspx");
                    }
                }
                else
                {
                    context.Response.Write("{\"status\": 1, \"name\": \"" + nc.name + "\"}");
                }
            }
            else
            {
                context.Response.Write("{\"status\": 1, \"name\": \"\"}");
            }
        }

        private static void login(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string nccode = ComLib.QueryStringHelper.GetFormString("nccode");
            qr_QrCodeRelate relate = qr_QrCodeRelateService.Instance.Get_SingleEntity_byWhere(
                qr_QrCodeRelate._.rel_Code == nccode);
            if (relate != null)
            {
                qr_QrCode qr = qr_QrCodeService.Instance.Get_SingleEntity_byWhere(qr_QrCode._.scene_id == relate.qr_Scene_ID);
                if (qr != null)
                {
                    context.Response.Write("{\"status\": 0, \"code_url\": \"" + qr.qrcode_url + "\"}");
                    //context.Response.Redirect("show.aspx");
                }
                else
                {
                    context.Response.Write("{\"status\": 1, \"code_url\": \"\"}");
                }
            }
            else
            {
                context.Response.Write("{\"status\": 1, \"code_url\": \"\"}");
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}